<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2010 Grzegorz Bańka <grzegorz.banka@gmail.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Plugin 'MT prize counter' for the 'mt_prize' extension.
 *
 * @author	Grzegorz Bańka <grzegorz.banka@gmail.com>
 * @package	TYPO3
 * @subpackage	tx_mtprize
 */
class tx_mtprize_pi1 extends tslib_pibase {
	var $prefixId      = 'tx_mtprize_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_mtprize_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey        = 'mt_prize';	// The extension key.
	var $pi_checkCHash = true;
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	The content that is displayed on the website
	 */
	function main($content, $conf) {
		$this->conf = $conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
			
		//return $this->pi_wrapInBaseClass($content);
	}
	function del_points($db_table,$db_table_uid,$feuser)
	{
		$feuser=intval($feuser);
		$where = 'disable=0 AND uid='.$feuser;
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_mtprize_mtprize','fe_users',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$tx_mtprize_mtprize=$row['tx_mtprize_mtprize'];
		}
		
		$where='what_columnt="'.$db_table.'" AND what_uid="'.$db_table_uid.'" AND fe_user="'.$feuser.'"';
		$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,point','tx_mtprize_history',$where);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$uid_del = $row['uid'];
			$point=$row['point'];
		}
		$tx_mtprize_mtprize=$tx_mtprize_mtprize-$point;

		$del_query=array('deleted'=>1,'tstamp' => time());
		$query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_mtprize_history', 'uid="'.$uid_del.'"', $del_query);		
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
		
		$del_query=array('tx_mtprize_mtprize' => $tx_mtprize_mtprize);
		$query = $GLOBALS['TYPO3_DB']->UPDATEquery('fe_users', 'uid="'.$feuser.'"', $del_query);		
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
		
			//print_r($del_query);exit;
		
	}
	function add_points($count,$feuser,$desc,$plus,$db_table,$db_table_uid,$pid)
	{
		/*
		$count=$this->config['points_count'];
		$feuser=$GLOBALS["TSFE"]->fe_user->user['uid'];
		$desc=$this->config['description'];
		$plus=$this->config['plus'];
		$db_table= $this->config['db_table'];
		$db_table_uid= $this->config['db_table_uid'];
		*/
		
		if(intval($count)>0 && intval($feuser)>0 && strlen($desc)>3 && strlen($db_table)>3 && intval($db_table_uid)!=0)
		{

			$error=1;
			$where = 'disable=0 AND uid='.$feuser;
			$res =  $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,tx_mtprize_mtprize','fe_users',$where);
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{
				$error=0;
				$tx_mtprize_mtprize=$row['tx_mtprize_mtprize'];
			}
			if($error==0)
			{
				if($plus==1) $tx_mtprize_mtprize=$tx_mtprize_mtprize+$count;
				else $tx_mtprize_mtprize=$tx_mtprize_mtprize-$count;
				if($tx_mtprize_mtprize<=0) $tx_mtprize_mtprize=0;
				
				$add_query=array('tx_mtprize_mtprize'=>$tx_mtprize_mtprize);
				$query = $GLOBALS['TYPO3_DB']->UPDATEquery('fe_users', 'uid="'.$feuser.'"', $add_query);		
				$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);	
			
				$query=array(
					'tstamp'	=>	time(),
					'crdate'	=>	time(),
					'pid'=>$pid,
					'point'	=>	$count,
					'fe_user'	=>	$feuser,
					'description'	=>	$desc,
					'add_point'	=>	$plus,
					'what_columnt'	=>	$db_table,
					'what_uid'	=>	$db_table_uid
				);
				$GLOBALS['TYPO3_DB']->exec_INSERTquery(
						'tx_mtprize_history',
						$query
				);
			}

			//$uid_news=$GLOBALS['TYPO3_DB']->sql_insert_id();
		}
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_prize/pi1/class.tx_mtprize_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_prize/pi1/class.tx_mtprize_pi1.php']);
}

?>